package org.wzys.Service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wzys.Mapper.CarItemMapper;
import org.wzys.Mapper.ProductMapper;
import org.wzys.Pojo.PageResult;
import org.wzys.Pojo.Product;
import org.wzys.Pojo.ProductQueryparm;
import org.wzys.Pojo.User;
import org.wzys.Service.ProductService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceimpl implements ProductService {
    
    
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CarItemMapper carItemMapper;
    @Override
    public PageResult getProductpage(ProductQueryparm productQueryparm) {
        //实现分页查询
        //1. 设置PageHelper分页参数
        PageHelper.startPage(productQueryparm.getPagenum(), productQueryparm.getPageSize());
        //2. 执行查询
        List<Product> ProductList = productMapper.list(productQueryparm);
        //3. 封装分页结果
        Page<Product> p = (Page<Product>) ProductList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public void addProduct(Product product) {
      product.setUpdateTime(LocalDateTime.now());
      productMapper.insert(product);
    }

    @Override
    public void deleteProduct(List<Integer> ids) {
        productMapper.deleteBatch(ids);
    }

    @Override
    public Product getProductById(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void addtocar(Integer productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        product.setUpdateTime(LocalDateTime.now());
        carItemMapper.add(product);

    }
}
